#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <setjmp.h>

#include "debug.h"
#include "interface.h"
#include "app.h"
#include "func_app.h"
#include "login_app.h"

/*	好友界面名字属性 */
char *LABEL_FORMAT	= {"<span foreground=\"#25b9b4\""		/* RGB颜色数值 */
					   "font=\"13\"				"			/* 字体大小    */
					   "font_weight=\"bold\">%s</span>" }; 	/* 字体（锐利\加厚等）属性*/

static struct view g_view = {
	.login_view = login_view,
	.regist_view = regist_view,
	.friend_view = friend_view,
	.chat_view = chat_view,
}; 

//user login in view interaction event handle.
static int login_click_login_button(GtkWidget *orign, GdkEvent *event,
							  struct interface *p_interface);
static int login_click_iconify(GtkWidget *orign, GdkEvent *event,
							   struct interface *p_interface);
static int login_click_close(GtkWidget *orign, GdkEvent *event,
							 struct interface *p_interface);
static int login_click_regist_button(GtkWidget *orign, GdkEvent *event,
							   struct interface *p_interface);

//user register view interaction event handle.
static int sign_click_iconify(GtkWidget *orign, GdkEvent *event,
							  struct interface *p_interface);
static int sign_click_close(GtkWidget *orign, GdkEvent *event,
							struct interface *p_interface);
static int sign_click_regist_button(GtkWidget *orign, GdkEvent *event,
									struct interface *p_interface);
static int sign_click_return_button(GtkWidget *orign, GdkEvent *event, 
									struct interface *p_interface);

//user friend view interaction event handle.
static int friend_click_iconify(GtkWidget *orign, GdkEvent *event, 
								struct interface *p_interface);
static int friend_click_close(GtkWidget *orign, GdkEvent *event, 
							  struct interface *p_interface);
static int friend_click_add(GtkWidget *orign,GdkEvent *event,
							struct interface *p_interface);
static int friend_click_remove(GtkWidget *orign, GdkEvent *event,
							   struct interface *p_interface);
static void double_click_friend_name(GtkWidget *widget,
									 GtkTreePath *path,
							   		 GtkTreeViewColumn *column,
							   		 struct interface *interfaced);
static int friend_view_treelist(struct interface *interfaced);
static inline int tree_insert_node(GtkTreeStore *treestore,
		    					   GtkTreeIter *iter_child,
	                     		   GtkTreeIter *iter_parent,
								   char *text);

//user chat view interaction event handle.
static int chat_click_iconify(GtkWidget *orign, GdkEvent *event, 
							  struct interface *p_interface);
static int chat_click_close(GtkWidget *orign, GdkEvent *event, 
							struct interface *p_interface);
static int chat_click_sent_button(GtkWidget *orign, GdkEvent *event, 
 	  							  struct interface *p_interface);

static void container_delete_all_children(GtkContainer * container);


int view_run(struct interface *p_interface)
{

	gtk_init(NULL, NULL);

	p_interface->view.next_view = LOGIN_V;
		
	view_manage(p_interface);

	gtk_main();
	
	return 0;
}


int view_manage(struct interface *p_interface)
{

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	switch (p_interface->view.next_view) {
	case LOGIN_V:
		g_view.login_view(p_interface);
		break;

	case SIGN_V:
		g_view.regist_view(p_interface);
		break;

	case FRIEND_V:
		g_view.friend_view(p_interface);
		break;
	case CHAT_V:
		g_view.chat_view(p_interface);
		break;		
	default:			
		break;
	}

	return true;
}


int login_view(struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	struct login_v *d = &p_interface->view.login_view_data;

	d->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	d->fixed_box = gtk_fixed_new();
	d->eventbox_login = gtk_event_box_new();
	d->eventbox_iconify = gtk_event_box_new();
	d->eventbox_close = gtk_event_box_new();
	d->eventbox_register = gtk_event_box_new();
	d->hint_lable = gtk_label_new("");
	d->button_register = gtk_label_new("注册账号");

	d->oicq_logo_image = gtk_image_new_from_file(IMAGE_FILE_DIR
									"/login/logo");
	d->portrait_image = gtk_image_new_from_file(IMAGE_FILE_DIR
									"/login/portrait");
	d->background = gtk_image_new_from_file(IMAGE_FILE_DIR
									"/login/backg");
	d->button_login = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/login/login_button");
	d->button_iconify = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/login/iconify");
	d->button_close = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/login/close");

	d->entry_buffer_text_account = gtk_entry_buffer_new ("", -1);
	d->entry_buffer_text_password = gtk_entry_buffer_new ("", -1);
	d->entry_account = gtk_entry_new_with_buffer(d->entry_buffer_text_account);
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_account), ACCOUNT_LENGTH - 1);
	d->entry_password = gtk_entry_new_with_buffer(d->entry_buffer_text_password);
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_password), PASSWORD_LENGTH -1);
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_account), " 账号 ");	
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_password), " 密码 ");	
	gtk_entry_set_visibility(GTK_ENTRY(d->entry_password), 0);

	gtk_window_set_position(GTK_WINDOW(d->window), 
										GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(d->window), "窗口一");
	gtk_widget_set_size_request(d->window, 428, 336);
	gtk_window_set_decorated(GTK_WINDOW(d->window), FALSE);
	
	gtk_label_set_use_underline (GTK_LABEL(d->button_register), 0);
	
	gtk_container_add( GTK_CONTAINER(d->window), d->fixed_box);	
	gtk_container_add( GTK_CONTAINER(d->eventbox_login), d->button_login);

	gtk_container_add( GTK_CONTAINER(d->eventbox_iconify), d->button_iconify);
	gtk_container_add( GTK_CONTAINER(d->eventbox_close), d->button_close);
	gtk_container_add( GTK_CONTAINER(d->eventbox_register), d->button_register);

	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->background, 0, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->oicq_logo_image, 10, 10);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->portrait_image, 178, 88);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), 
						d->hint_lable, 131,147);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), 
						d->entry_account, 131,166);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), 
						d->entry_password, 131, 214);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_login, 118, 272);	
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_iconify, 370, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_close, 400, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_register, 10, 300);

	gtk_widget_add_events(d->eventbox_login, GDK_BUTTON_PRESS_MASK);
	gtk_widget_add_events(d->eventbox_iconify, GDK_BUTTON_PRESS_MASK);
	gtk_widget_add_events(d->eventbox_close, GDK_BUTTON_PRESS_MASK);
	
	g_signal_connect(d->eventbox_login, "button-press-event", 
						G_CALLBACK(login_click_login_button), p_interface);
	g_signal_connect(d->eventbox_iconify, "button-press-event", 
						G_CALLBACK(login_click_iconify), p_interface);
	g_signal_connect(d->eventbox_close, "button-press-event", 
						G_CALLBACK(login_click_close), p_interface);
	g_signal_connect(d->eventbox_register, "button-press-event", 
						G_CALLBACK(login_click_regist_button), p_interface);

	gtk_widget_show_all(d->window);

	return TRUE;
}

static int login_click_regist_button(GtkWidget *orign,
						GdkEvent *event, struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	container_delete_all_children(GTK_CONTAINER(p_interface->view.login_view_data.window));
	gtk_widget_destroy(p_interface->view.login_view_data.window);
	p_interface->view.next_view = SIGN_V;
	view_manage(p_interface);

	return TRUE;
}

static int login_click_login_button(GtkWidget *orign,
				   GdkEvent *event, struct interface *p_interface)
{
	int ret = 0;
	const gchar *account = NULL;
	const gchar *password = NULL;

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	/*获取账号密码*/
	account = gtk_entry_get_text(GTK_ENTRY(p_interface->view.login_view_data.entry_account));
	password = gtk_entry_get_text(GTK_ENTRY(p_interface->view.login_view_data.entry_password));

	strcpy(p_interface->p_app->user_login_info.account, 
		   (char *)account);
	strcpy(p_interface->p_app->user_login_info.password,
		   (char *)password);

	ret = user_login(&p_interface->p_app->user_login_info, 
					 &p_interface->p_app->mutexfd);
	if (ret == TRUE) {
		/*登陆成功，加载好友*/
		load_friend(&p_interface->p_app->mutexfd,
					p_interface->p_app->user_login_info.account,
					&p_interface->p_app->friend_list_head);

		container_delete_all_children(GTK_CONTAINER(p_interface->view.login_view_data.window));
		gtk_widget_destroy(p_interface->view.login_view_data.window);
		p_interface->view.next_view = FRIEND_V;
		view_manage(p_interface);
		return TRUE;
	}
						
	switch(ret) {
		case LOGIN_ACCOUNT_WRONG_FROMAT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">账号格式错误</span>"));
			break;
		
		case LOGIN_PASSWORD_WRONG_FROMAT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">密码格式错误</span>"));
			break;

		case -LOGIN_INFO_EMPTY_ERR:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">有信息未输入</span>"));
			break;

		case LOGIN_ACCOUNT_NOT_EXIXT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">账号未注册</span>"));
			break;

		case LOGIN_PASSWORD_INCORRECT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">密码错误</span>"));
			break;

		case LOGIN_TOO_MUCH:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">服务器繁忙，请稍后再试</span>"));
			break;

		case LOGIN_ACCOUNT_HAS_BEEN_LOGIN:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.login_view_data.hint_lable),
				("<span foreground=\"#FF0000\">账号已被登陆</span>"));
			break;

		default:
			break;
	}

	return TRUE;
}


static int login_click_iconify(GtkWidget *orign, 
						GdkEvent *event, struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_iconify(GTK_WINDOW(p_interface->view.login_view_data.window));

	return TRUE;
}


static int login_click_close(GtkWidget *orign,
						 GdkEvent *event, struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_close(GTK_WINDOW(p_interface->view.login_view_data.window));
	gtk_main_quit();

	return TRUE;
}


int regist_view(struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	struct regist_v *d = &p_interface->view.regist_view_data;
	
	d->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	d->fixed_box = gtk_fixed_new();
	d->eventbox_iconify = gtk_event_box_new();
	d->eventbox_close = gtk_event_box_new();

	d->hint_lable = gtk_label_new("");
	d->label_name = gtk_label_new("昵 称:");
	d->label_account = gtk_label_new("账 号:");
	d->label_password = gtk_label_new("密 码:");
	d->label_password_again = gtk_label_new("再 次 输 入:");
	d->button_register = gtk_button_new_with_label("\t\t\t  注   册\t\t\t\t");
	d->button_return = gtk_button_new_with_label("\t\t\t  返   回\t\t\t\t");

	d->entry_name = gtk_entry_new();
	d->entry_account = gtk_entry_new();
	d->entry_password = gtk_entry_new();
	d->entry_password_again = gtk_entry_new();
		
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_name),
									   NAME_LENGTH - 1);
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_account),
									   ACCOUNT_LENGTH - 1);
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_password), 
									   PASSWORD_LENGTH - 1);
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_password_again), 
									   PASSWORD_LENGTH - 1);
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_name),
								   " 昵称:10位数字或英文");	
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_account),
								   " 账号:10位数字或英文");	
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_password),
								   " 密码:10位数字或英文");
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_password_again),
								   " 再次输入密码 ");	
	gtk_entry_set_visibility(GTK_ENTRY(d->entry_password), FALSE);
	gtk_entry_set_visibility(GTK_ENTRY(d->entry_password_again), FALSE);
	gtk_window_set_position(GTK_WINDOW(d->window), 
										GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(d->window), "窗口一");
	gtk_widget_set_size_request(d->window, 293, 637);
	gtk_window_set_decorated(GTK_WINDOW(d->window), FALSE);

	d->background = gtk_image_new_from_file(IMAGE_FILE_DIR
									"/sign/backg");
	d->button_iconify = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/sign/iconify");
	d->button_close = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/sign/close");

	gtk_container_add(GTK_CONTAINER(d->window), d->fixed_box);	
	gtk_container_add(GTK_CONTAINER(d->eventbox_iconify),
					  d->button_iconify);
	gtk_container_add(GTK_CONTAINER(d->eventbox_close),
					  d->button_close);

	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->background, 0, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_iconify, 236, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_close, 267, 0);

	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->hint_lable, 45, 370);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->label_name, 50, 100);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->label_account, 50, 160);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->label_password, 50, 220);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->label_password_again, 15, 280);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->entry_name, 95, 93);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->entry_account, 95, 153);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->entry_password, 95, 213);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->entry_password_again, 95, 273);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->button_register, 45, 400);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->button_return, 45, 470);


	gtk_widget_add_events(d->eventbox_iconify, GDK_BUTTON_PRESS_MASK);
	gtk_widget_add_events(d->eventbox_close, GDK_BUTTON_PRESS_MASK);

	g_signal_connect(d->eventbox_iconify, "button-press-event", 
						G_CALLBACK(sign_click_iconify), p_interface);
	g_signal_connect(d->eventbox_close, "button-press-event", 
						G_CALLBACK(sign_click_close), p_interface);
	g_signal_connect(d->button_register,"clicked",
						G_CALLBACK(sign_click_regist_button), p_interface);
	g_signal_connect(d->button_return,"clicked",
						G_CALLBACK(sign_click_return_button), p_interface);
	
	gtk_widget_show_all(d->window);

	return true;
}


static int sign_click_iconify(GtkWidget *orign, GdkEvent *event, 
							  struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_iconify(GTK_WINDOW(p_interface->view.regist_view_data.window));

	return TRUE;
}


static int sign_click_close(GtkWidget *orign, GdkEvent *event, 
							struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_close(GTK_WINDOW(p_interface->view.regist_view_data.window));
	gtk_main_quit();
	return TRUE;
}

static int sign_click_regist_button(GtkWidget *orign, GdkEvent *event, 
									struct interface *p_interface)
{
	int ret = 0;
	const gchar *name = NULL;
	const gchar *account = NULL;
	const gchar *password = NULL;
	const gchar *password_again = NULL;

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	/*获取昵称，账号，密码*/
	name = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.regist_view_data.entry_name));
	account = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.regist_view_data.entry_account));
	password = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.regist_view_data.entry_password));
	password_again = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.regist_view_data.entry_password_again));

	 if (strcmp((char *)password, (char *)password_again)) {
		gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">两次密码不相同</span>"));
		return TRUE;
	} 

	strcpy(p_interface->p_app->user_login_info.name, 
		   (char *)name);
	strcpy(p_interface->p_app->user_login_info.account, 
		   (char *)account);
	strcpy(p_interface->p_app->user_login_info.password,
		   (char *)password);

	ret = user_register(p_interface->p_app->user_login_info, 
					 	&p_interface->p_app->mutexfd);
	switch(ret) {
		case TRUE:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">注册成功！</span>"));
			break;

		case -LOGIN_INFO_EMPTY_ERR :
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">有信息未输入</span>"));
			break;

		case LOGIN_NAME_WRONG_FROMAT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">昵称格式错误</span>"));
			break;

		case LOGIN_ACCOUNT_WRONG_FROMAT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">账号格式错误</span>"));
			break;

		case LOGIN_PASSWORD_WRONG_FROMAT:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">密码格式错误</span>"));
			break;

		case LOGIN_ACCOUNT_EXIST:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.regist_view_data.hint_lable),
				("<span foreground=\"#FF0000\">账号已被注册</span>"));
			break;

		default:
			break;
	}
	
	return TRUE;
}

static int sign_click_return_button(GtkWidget *orign, GdkEvent *event, 
									struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	container_delete_all_children(GTK_CONTAINER(p_interface->view.regist_view_data.window));
	gtk_widget_destroy(p_interface->view.regist_view_data.window);
	p_interface->view.next_view = LOGIN_V;
	view_manage(p_interface);

	return TRUE;
}

/*好友界面窗口*/
int friend_view(struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	struct friend_v *d = &p_interface->view.friend_view_data;
	char *markup = NULL;	

	d->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	d->fixed_box = gtk_fixed_new();
	d->treelist_window = gtk_scrolled_window_new(NULL, NULL);
	d->eventbox_iconify = gtk_event_box_new();
	d->eventbox_close = gtk_event_box_new();
	d->label_name = gtk_label_new(NULL);
	d->friend_treelist = (struct tree*) malloc (sizeof(struct tree));
 
	d->background = gtk_image_new_from_file(IMAGE_FILE_DIR
									"/friend/backg");
	d->button_iconify = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/friend/iconify");
	d->button_close = gtk_image_new_from_file(IMAGE_FILE_DIR 
									"/friend/close");

	d->label_friend_account = gtk_label_new("好友账号:");
	d->entry_friend_account = gtk_entry_new();
	d->button_add = gtk_button_new_with_label("  添 加  ");
	d->button_remove = gtk_button_new_with_label("  删 除  ");
	d->hint_lable = gtk_label_new("");
	gtk_entry_set_max_length(GTK_ENTRY(d->entry_friend_account),
									   ACCOUNT_LENGTH - 1);
	gtk_entry_set_placeholder_text(GTK_ENTRY(d->entry_friend_account),
								   " 好友账号");

	gtk_window_set_position(GTK_WINDOW(d->window), GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(d->window), "好友表");
	gtk_widget_set_size_request(d->window, 300, 675);
	//gtk_window_set_decorated(GTK_WINDOW(d->window), FALSE);
	gtk_window_move(GTK_WINDOW(d->window), 1400, 150);
 
	gtk_container_add(GTK_CONTAINER(d->window), d->fixed_box);	
	gtk_container_add(GTK_CONTAINER(d->eventbox_iconify), d->button_iconify);
	gtk_container_add(GTK_CONTAINER(d->eventbox_close), d->button_close);
	
	markup = g_markup_printf_escaped(LABEL_FORMAT,
							p_interface->p_app->user_login_info.name);	
	gtk_label_set_markup(GTK_LABEL(d->label_name), markup);

	gtk_fixed_put(GTK_FIXED(d->fixed_box), d->background, 0, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), d->eventbox_iconify, 251, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), d->eventbox_close, 278, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), d->treelist_window, 0, 160);
	gtk_fixed_put(GTK_FIXED(d->fixed_box), d->label_name, 150, 85);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
				  d->label_friend_account, 15, 633);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
				  d->entry_friend_account, 85, 626);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
				  d->button_add, 100, 670);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
				  d->button_remove, 185, 670);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
				  d->hint_lable, 85, 605);

	d->friend_treelist->window = d->treelist_window;	
	friend_view_treelist(p_interface);		
	
	gtk_widget_add_events(d->eventbox_iconify, GDK_BUTTON_PRESS_MASK);
	gtk_widget_add_events(d->eventbox_close, GDK_BUTTON_PRESS_MASK);

	g_signal_connect(d->eventbox_iconify, "button-press-event", 
						G_CALLBACK(friend_click_iconify), p_interface);
	g_signal_connect(d->eventbox_close, "button-press-event", 
						G_CALLBACK(friend_click_close), p_interface);
	g_signal_connect(d->button_add,"clicked",
					G_CALLBACK(friend_click_add), p_interface);
	g_signal_connect(d->button_remove,"clicked",
					G_CALLBACK(friend_click_remove), p_interface);
	
	gtk_widget_show_all(d->window);

	return true;
}

/*创建一个列表*/
static int friend_view_treelist(struct interface *interfaced)
{
	
	struct tree *d = interfaced->view.friend_view_data.friend_treelist;
	
	/* 创建一个视图显示数据*/
	d->treeview = gtk_tree_view_new();
	d->treestore = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING);
	d->tree_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(d->treeview));
	d->renderer = gtk_cell_renderer_text_new();
	d->col = gtk_tree_view_column_new();

	gtk_cell_renderer_set_fixed_size(d->renderer, ITEM_SELL_L, ITEM_SELL_H);

	gtk_tree_view_column_set_title(d->col, "\t\t\t\t      联  系  人");
	/*往视图加一列*/
	gtk_tree_view_append_column(GTK_TREE_VIEW(d->treeview), d->col);
	gtk_tree_view_column_pack_start(d->col, d->renderer, TRUE);
	gtk_tree_view_column_add_attribute(d->col, d->renderer,
										  "text", 0);

	/*往列表写写数据*/
	updata_friend_store(interfaced);
	d->model = GTK_TREE_MODEL(d->treestore);
	gtk_tree_view_set_model(GTK_TREE_VIEW(d->treeview), d->model);
	g_object_unref(d->model);
	
	gtk_widget_set_size_request(d->window, SCROOL_WINDOW_L, SCROOL_WINDOW_H);
	gtk_container_add(GTK_CONTAINER(d->window),d->treeview);

	g_signal_connect(GTK_TREE_VIEW(d->treeview), "row-activated",
					 G_CALLBACK(double_click_friend_name), interfaced);

	return TRUE;
}

/*点击添加按钮触发函数*/
static int friend_click_add(GtkWidget *orign,
						 GdkEvent *event, struct interface *p_interface)
{
	int ret = 0;
	const gchar *input = NULL;
	char friend_account[ACCOUNT_LENGTH];

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	/*获取界面上的好友账号*/
	input = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.friend_view_data.entry_friend_account));
	memset(friend_account, '\0', ACCOUNT_LENGTH);
	strcpy(friend_account, input);

	ret = add_friend(p_interface->p_app->user_login_info.account,
					 friend_account,
					 &p_interface->p_app->friend_list_head,
					 &p_interface->p_app->mutexfd);
	if (ret == TRUE) {
		/*添加成功刷新好友表*/
		updata_friend_store(p_interface);
		return TRUE;
	}

	switch (ret) {
		case -FUNCAPP_PARAM_NULL_ERR:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">请输入</span>"));
			break;

		case FUNCAPP_FRIEND_IS_OWNER:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">不能添加自己为好友</span>"));
			break;

		case FUNCAPP_ADD_FAIL:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">添加失败-服务器出错</span>"));
			break;

		case FUNCAPP_FRIEND_ACCOUNT_NOT_EXIST:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">该账号不存在</span>"));
			break;

		case FUNCAPP_EXCEED_FRIEND_NUM:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">只能添加20个好友</span>"));
			break;

		case FUNCAPP_IS_ALREADY_FRIEND:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">已经添加该账号为好友</span>"));
			break;

		default:
			break;
	}

	return TRUE;
}

static int friend_click_remove(GtkWidget *orign,
						 	   GdkEvent *event, struct interface *p_interface)
{
	int ret = 0;
	const gchar *input = NULL;
	char friend_account[ACCOUNT_LENGTH];

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	/*获取界面上的好友账号*/
	input = gtk_entry_get_text(
		GTK_ENTRY(p_interface->view.friend_view_data.entry_friend_account));
	memset(friend_account, '\0', ACCOUNT_LENGTH);
	strcpy(friend_account, input);

	ret = remove_friend(p_interface->p_app->user_login_info.account,
					    friend_account,
					    &p_interface->p_app->friend_list_head,
						&p_interface->p_app->mutexfd);
	if (ret == TRUE) {
		/*删除成功刷新好友表*/
		updata_friend_store(p_interface);
		return TRUE;
	}

	switch (ret) {
		case -FUNCAPP_PARAM_NULL_ERR:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">请输入</span>"));
			break;

		case FUNCAPP_FRIEND_IS_OWNER:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">不能删除自己</span>"));
			break;

		case FUNCAPP_REMOVE_FAIL:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">删除失败-服务器出错</span>"));
			break;

		case FUNCAPP_FRIEND_ACCOUNT_NOT_EXIST:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">该账号不存在</span>"));
			break;

		case FUNCAPP_NOT_FRIEND:
			gtk_label_set_markup(GTK_LABEL(p_interface->view.friend_view_data.hint_lable),
				("<span foreground=\"#FF0000\">未添加该账号为好友</span>"));
			break;

		default:
			break;
	}

	return TRUE;
}

static void double_click_friend_name(GtkWidget *widget, GtkTreePath *path,
							   		 GtkTreeViewColumn *column,
							   		 struct interface *interfaced)
{
	char *item_value;
	char *tmp;
	GtkTreeIter iter; 

	if (!interfaced) {
		DEBUG("PARAM NULL\n");
		return;
	}

	struct tree *d = interfaced->view.friend_view_data.friend_treelist;
	
	gtk_tree_selection_get_selected(GTK_TREE_SELECTION(d->tree_selection),
									&d->model, &iter);					 
	gtk_tree_model_get(d->model, &iter, COLUMN, &item_value, -1);

	/*找到‘—‘的位置*/
	tmp = strchr(item_value, '-');
	strcpy(interfaced->chating_friend_account, tmp + 1);

	/*一次只能打开一个窗口*/
	if (interfaced->view.next_view == CHAT_V) 
		gtk_window_close(GTK_WINDOW(interfaced->view.chat_view_data.window));

	interfaced->view.next_view = CHAT_V;
	view_manage(interfaced);

   	g_free(item_value);
}

int updata_friend_store(struct interface *interfaced)
{
	struct list_head *head, *next;
	struct friend *p_friend_tmp;
	struct tree *d = interfaced->view.friend_view_data.friend_treelist;
	char friend_info[NAME_LENGTH + ACCOUNT_LENGTH];
 
	/*将之前的数据清空*/
	gtk_tree_store_clear(d->treestore);
	/*假装建一个标题*/
	tree_insert_node(d->treestore, &d->iter_level_1, NULL,"  好友昵称-账号");

	memset(friend_info, '\0', 21);

	/*好友链表头*/
	head = &interfaced->p_app->friend_list_head;
	list_for_each(next, head) {
		p_friend_tmp = list_entry(next, struct friend, list);
		friend_info[0] = '\t';
		strcpy(friend_info + 1, p_friend_tmp->info.name);
		friend_info[strlen(friend_info)] = '-';
		strcpy(friend_info + strlen(friend_info), p_friend_tmp->info.account);
		tree_insert_node(d->treestore, &d->iter_level_2,
						 &d->iter_level_1, friend_info);
		memset(friend_info, '\0', 21);
	}
	
	return TRUE;
} 

/*
**	在树中插入元素
**	参数：
**		treestore 	树的数据存储区
**		iter_child	子节点本身指针
**		iter_parent		母节点指针
**/
static inline int tree_insert_node(GtkTreeStore *treestore,
						  		   GtkTreeIter *iter_child,
	   			                   GtkTreeIter *iter_parent,
						 		   char *text)
{
	gtk_tree_store_append(treestore, iter_child, iter_parent);
  	gtk_tree_store_set(treestore, iter_child,
                       COLUMN, text, -1);
	return TRUE;
}

static int friend_click_iconify(GtkWidget *orign,
						 GdkEvent *event, struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_iconify(GTK_WINDOW(p_interface->view.friend_view_data.window));

	return TRUE;
}

static int friend_click_close(GtkWidget *orign, GdkEvent *event, struct interface *p_interface)
{	
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	free(p_interface->view.friend_view_data.friend_treelist);
	gtk_window_close(GTK_WINDOW(p_interface->view.friend_view_data.window));
	gtk_main_quit();

	return TRUE;
}


int chat_view(struct interface *p_interface)
{
	int ret = 0;
	int count = 0;
	struct chat_packet chat_packet;

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	struct chat_v *d = &p_interface->view.chat_view_data;
	
	d->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	d->scrolled_window_edit = gtk_scrolled_window_new(NULL, NULL);
	d->scrolled_window_history = gtk_scrolled_window_new(NULL, NULL);
	d->fixed_box = gtk_fixed_new();
	d->fixed_box_history = gtk_fixed_new();
	d->fixed_box_edit = gtk_fixed_new();
	d->eventbox_iconify = gtk_event_box_new();
	d->eventbox_close = gtk_event_box_new();
	
	d->button_send = gtk_button_new_with_label
									("\t\t发     送\t\t\t");
	d->background = gtk_image_new_from_file(IMAGE_FILE_DIR
											"/chat/backg");
	d->button_iconify = gtk_image_new_from_file(IMAGE_FILE_DIR 
											"/chat/iconify");
	d->button_close = gtk_image_new_from_file(IMAGE_FILE_DIR 
											"/chat/close");
	d->background_edit = gtk_image_new_from_file(IMAGE_FILE_DIR
											"/chat/backg_edit");
	
	d->text_view_edit = gtk_text_view_new();
	gtk_widget_set_size_request(d->text_view_edit, 719, 110);
	gtk_widget_set_size_request(d->scrolled_window_edit, 719, 110);
	gtk_container_add(GTK_CONTAINER(d->scrolled_window_edit),
										  d->text_view_edit);
	d->text_view_history = gtk_text_view_new();
	gtk_widget_set_size_request(d->text_view_history, 718, 325);
	gtk_widget_set_size_request(d->scrolled_window_history, 718, 325);
	gtk_container_add(GTK_CONTAINER(d->scrolled_window_history),
										  d->text_view_history);
	
	d->buffer_edit = gtk_text_view_get_buffer(GTK_TEXT_VIEW(d->text_view_edit));
	d->buffer_history = gtk_text_view_get_buffer(GTK_TEXT_VIEW(d->text_view_history));

	gtk_text_view_set_editable(GTK_TEXT_VIEW(d->text_view_history),FALSE);	

	gtk_window_set_position(GTK_WINDOW(d->window), 
							GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW(d->window), "窗口一");
	gtk_widget_set_size_request(d->window, 719, 519);
	//gtk_window_set_decorated(GTK_WINDOW(d->window), FALSE);
	gtk_window_move(GTK_WINDOW(d->window), 600,230);
	gtk_container_add(GTK_CONTAINER(d->window),
 					  d->fixed_box);	
	gtk_container_add(GTK_CONTAINER(d->eventbox_iconify), 
					  d->button_iconify);
	gtk_container_add(GTK_CONTAINER(d->eventbox_close),
					 d->button_close);
	
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->background, 0, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_iconify, 656, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->eventbox_close, 687, 0);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->fixed_box_history, 0, 31);
	gtk_fixed_put(GTK_FIXED(d->fixed_box),
						d->fixed_box_edit, 0, 372);
	gtk_fixed_put(GTK_FIXED(d->fixed_box_edit),
						d->background_edit, 0, 0);

	gtk_fixed_put(GTK_FIXED(d->fixed_box_history),
						d->scrolled_window_history, 2, 10);
	gtk_fixed_put(GTK_FIXED(d->fixed_box_edit),
						d->scrolled_window_edit, 2, 15);
	gtk_fixed_put(GTK_FIXED(d->fixed_box_edit),
						d->button_send, 530, 110);

	/* 加载聊天记录 */
	chat_packet.send_account = p_interface->p_app->user_login_info.account;
	chat_packet.recv_account = p_interface->chating_friend_account;
	gtk_text_buffer_get_bounds(d->buffer_history, &d->history_start, &d->history_end);
	for (count = 15; count > 0; count--) {
		ret = get_chat_histroy(&chat_packet, count);
		if (ret == TRUE) {
			gtk_text_buffer_insert(d->buffer_history,
								   &d->history_end, chat_packet.send_name, -1);
			gtk_text_buffer_insert(d->buffer_history, 
						   		   &d->history_end, "\t", -1);
			gtk_text_buffer_insert(d->buffer_history, 
								   &d->history_end, chat_packet.send_time, -1);
			gtk_text_buffer_insert(d->buffer_history, 
								   &d->history_end, "\n\t", -1);
			gtk_text_buffer_insert(d->buffer_history, 
								   &d->history_end, chat_packet.msg, -1);
			gtk_text_buffer_insert(d->buffer_history, 
								   &d->history_end, "\n", -1);
			free(chat_packet.send_name);
			free(chat_packet.send_time);
			free(chat_packet.msg);
		}
	}

	gtk_widget_add_events(d->eventbox_iconify, GDK_BUTTON_PRESS_MASK);
	gtk_widget_add_events(d->eventbox_close, GDK_BUTTON_PRESS_MASK);

	g_signal_connect(d->eventbox_iconify, "button-press-event", 
						G_CALLBACK(chat_click_iconify), p_interface);
	g_signal_connect(d->eventbox_close, "button-press-event", 
						G_CALLBACK(chat_click_close), p_interface);
	g_signal_connect(d->button_send, "clicked", 
						G_CALLBACK(chat_click_sent_button), p_interface);

	gtk_widget_show_all(d->window);

	return true;
}

static int chat_click_iconify(GtkWidget *orign, GdkEvent *event,
 								struct interface *p_interface)
{
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	gtk_window_iconify(GTK_WINDOW(p_interface->view.chat_view_data.window));

	return TRUE;
}

static int chat_click_close(GtkWidget *orign, GdkEvent *event,
								struct interface *p_interface)
{		
	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}
	/*这里有段错误，不知道为什么，放弃了*/
	p_interface->view.next_view = FRIEND_V;
	gtk_window_close(GTK_WINDOW(p_interface->view.chat_view_data.window));
	return TRUE;
}

static int chat_click_sent_button(GtkWidget *orign, GdkEvent *event, 
 	  							  struct interface *p_interface)
{
	struct chat_packet send_chat_packet;
	struct chat_packet recv_chat_packet;
	GtkTextIter edit_start;
	GtkTextIter edit_end;
	char *context_edit;		

	if (!p_interface) {
		DEBUG(" PARAM NULL!\n");
		return (-INTERFACE_PARAM_NULL);
	}

	struct chat_v *d = &p_interface->view.chat_view_data;
	send_chat_packet.send_name = p_interface->p_app->user_login_info.name;
	send_chat_packet.send_account = p_interface->p_app->user_login_info.account;
	send_chat_packet.recv_account = p_interface->chating_friend_account;
	recv_chat_packet.send_account = p_interface->p_app->user_login_info.account;
	recv_chat_packet.recv_account = p_interface->chating_friend_account;
	
	gtk_text_buffer_get_bounds(d->buffer_edit, &edit_start, &edit_end);
	
	/*从输入框获取信息并发送出去*/
	context_edit = gtk_text_buffer_get_text(d->buffer_edit, &edit_start, &edit_end, FALSE);
	send_chat_packet.msg = context_edit;
	send_msg(&p_interface->p_app->mutexfd,send_chat_packet);
  
	/* 将内容显示到历史框 */
	get_chat_histroy(&recv_chat_packet, 1);
	gtk_text_buffer_insert(d->buffer_history,
						   &d->history_end, recv_chat_packet.send_name, -1);
	gtk_text_buffer_insert(d->buffer_history, 
						   &d->history_end, "\t", -1);
	gtk_text_buffer_insert(d->buffer_history, 
						   &d->history_end, recv_chat_packet.send_time, -1);
	gtk_text_buffer_insert(d->buffer_history, 
						   &d->history_end, "\n\t", -1);
	gtk_text_buffer_insert(d->buffer_history, 
						   &d->history_end, recv_chat_packet.msg, -1);
	gtk_text_buffer_insert(d->buffer_history, 
						   &d->history_end, "\n", -1);
	free(recv_chat_packet.send_name);
	free(recv_chat_packet.send_time);
	free(recv_chat_packet.msg);
	
	gtk_text_buffer_delete(d->buffer_edit, &edit_start, &edit_end);

	return TRUE;
}

/*
 *	删除容器gtk_container上的所以的子部件
 */
static void container_delete_all_children(GtkContainer * container)
{	
	GList *glist = gtk_container_get_children (container);
	if(glist != NULL)
	{	gtk_container_remove(container,GTK_WIDGET(glist->data));
		while(glist->next != NULL)
		{
			gtk_container_remove(container,GTK_WIDGET(glist->next->data));
			glist = glist->next;
		}
	}
		glist = gtk_container_get_children (container);

	if(g_list_length (glist) == 0);
}

